package com.hellojava.util;

import com.hellojava.util.web.JsonResponse;
import com.hellojava.util.web.RequestContext;
import com.hellojava.util.web.ResponseHeader;
import com.hellojava.util.web.StandardResponseHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

public class WebUtil {
    static Logger logger = LoggerFactory.getLogger(WebUtil.class);

    public final static String REDIRECT_RETURN_URL = "redirectReturnUrl";

    public final static String OPEN_ID = "openid"; // 微信的openid 和 支付宝的user_id

    public final static String HTTP_PREFIX = "http"; // http前缀

    public final static String GET = "GET"; // GET方法

    /**
     * ajax请求重定向
     *
     * @param response
     * @param redirect
     */
    public static void ajaxRedirect(HttpServletResponse response, String redirect) {
        try {
            OutputStream outputStream = response.getOutputStream();
            response.setHeader("content-type", "text/html;charset=UTF-8");
            redirect = String.format("{\"redirect\": \"%s\"}", redirect);
            outputStream.write(redirect.getBytes("UTF-8"));
        } catch (Exception e) {
            logger.error("ajax request redirect error", e);
        }
    }

    /**
     * 普通请求重定向
     *
     * @param response
     * @param redirect
     * @throws Exception
     */
    public static void redirect(HttpServletResponse response, String redirect) throws Exception {
        if (RequestContext.getHttpMethodInfo().isAjax()) {
            ajaxRedirect(response, redirect);
        } else {
            response.sendRedirect(redirect);
        }
    }

    /**
     * 根据本应用的域名去重定向
     * @param request
     * @param response
     * @param redirect
     * @throws Exception
     */
    public static void redirectByDomain(HttpServletRequest request, HttpServletResponse response, String redirect) throws Exception {
        String domain = getDomainBasePath() + "/";

        if (RequestContext.getHttpMethodInfo().isAjax()) {
            ajaxRedirect(response, domain + redirect);
        } else {
            response.sendRedirect(domain + redirect);
        }
    }


    public static JsonResponse jsonResponse(ResponseHeader header, Object data) {
        return JsonResponse.newInstance(header, data);
    }

    public static JsonResponse jsonResponse(ResponseHeader header) {
        return jsonResponse(header, null);
    }

    public static JsonResponse successJsonResponse(String successMessage, Object data) {
        return JsonResponse.newInstance(StandardResponseHeader.SUCCESS, successMessage, data);
    }

    public static JsonResponse successJsonResponse() {
        return successJsonResponse(null);
    }

    public static JsonResponse successJsonResponse(String successMessage) {
        return successJsonResponse(successMessage, null);
    }

    public static JsonResponse errorJsonResponse(String errorMessage,Object data){
        return JsonResponse.newInstance(StandardResponseHeader.ERROR,errorMessage,data);
    }

    public static JsonResponse errorJsonResponse(String errorMessage){
        return errorJsonResponse(errorMessage,null);
    }

    public static JsonResponse tokenFail(String errorMessage){
        return JsonResponse.newInstance(StandardResponseHeader.ERROR_TOCKEN_EXPIRE,errorMessage,null);
    }


    /**
     * 获取服务器带域名的基础路径
     * 例如：http://cpsydltest.cunpiao.com
     * @return
     */
    public static String getDomainBasePath() {
        return RequestContext.getModuleProperties().domain;
    }




}
